package com.zb.sannuo.maintenance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zb.sannuo.common.domain.Menu;
import com.zb.sannuo.common.domain.search.SystemSettingsSearch;
import com.zb.sannuo.common.result.Result;
import com.zb.sannuo.common.result.ResultCode;
import com.zb.sannuo.maintenance.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/menu")
@Api(tags = "菜单管理模块", description = "对菜单进行CRUD操作")
public class MenuController {

    @Resource
    private MenuService menuServiceImpl;

    @RequestMapping(value = "listmenu",method = RequestMethod.GET)
    @ApiOperation(value = "查询菜单所有",notes = "查询菜单所有",httpMethod = "GET")
    public Result queryIdPatient(Integer page,Integer limit)throws Exception{

        Page page1 = menuServiceImpl.selectListMenu(page, limit);
        System.out.println(page1.getTotal());
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),page1.getRecords());
    }

    @RequestMapping(value = "{menuId}", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据ID删除用户", notes = "根据ID删除用户", httpMethod = "DELETE")
    public Result deleteUserById(@PathVariable("menuId") Integer menuId) throws Exception{
        //TODO:service方法
        int i = menuServiceImpl.deleteMenuById(menuId);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.ok(ResultCode.FAIL, null);
        }


    }


    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "增加功能",notes = "增加功能")
    public Result addMenu(Menu menu)throws Exception{
        menu.setUpdateTime(new Date());
        int i = menuServiceImpl.addMenus(menu);
        return Result.ok(ResultCode.SUCCESS,i);
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ApiOperation(value = "修改功能",notes = "修改功能")
    public Result updateMenu(Menu menu)throws Exception{
        int i = menuServiceImpl.updateMenus(menu);
        return Result.ok(ResultCode.SUCCESS,i);
    }

}
